package com.tfblog.blog.utils;

import com.auth0.jwt.JWT;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by FengBin on 2021/5/7 14:46
 * Token的工具类
 */
public class TokenUtils {
    public static String getTokenUsername() {
        String token = getRequest().getHeader("Authorization");
        //String token = getRequest().getParameter("Authorization");
        String username = JWT.decode(token).getAudience().get(0);
        return username;
    }


    /*
    * 获取请求体
    * */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }
}
